<!-- <template>
		<view class="add-btn" :style="{
			background: styleBtn.background,
			'box-shadow': styleBtn['box-shadow'],
			bottom: styleBtn.bottom
		}" @click="jumpUrl">
			<template v-if="text == ''">
				<u-icon name="plus" color="#fff"></u-icon>
			</template>
			<template v-else>
				<text class="btn-txt">{{text}}</text>
			</template>
		</view>
</template>

<script>
	export default {
		props: {
			text: {
				type: String,
				default: () => {return ''}
			},
			styleBtn: {
				type: Object,
				default: () => {
					return {
						background: 'linear-gradient(90deg, #02BE6A 0%, #009984 100%)',
						'box-shadow': '3rpx 5rpx 7rpx 0rpx rgba(11, 173, 87, 0.29)',
						bottom: '200rpx'
					}
				},
			},
		},
		data() {
			return {};
		},
		methods: {
			jumpUrl(){
				let _self = this
				_self.$emit('jump')
			},
		},
	}
</script>

<style lang="scss" scoped>
.add-btn{
	position: fixed;
	height: 110rpx;
	width: 110rpx;
	border-radius: 50%;
	right: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	.btn-txt{
		font-size: 26rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #FFF;
	}
}
</style> -->
<template>
	<view>
		<view
			id="_drag_button"
			class="drag"
			:style="{
				background: styleBtn.background,
				'box-shadow': styleBtn['box-shadow'],
				left: `${left}px`,
				top: `${top}px`,
			}"
			@touchstart="touchstart"
			@touchmove.stop.prevent="touchmove"
			@touchend.stop="touchend"
			@click.stop.prevent="jumpUrl"
			:class="{ transition: isDock && !isMove }"
		>
			<slot name="text">
				<template v-if="text == ''">
					<u-icon name="plus" color="#fff"></u-icon>
				</template>
				<template v-else>
					<text class="btn-txt">{{text}}</text>
				</template>
			</slot>
		</view>
	</view>
</template>

<script>
export default {
	name: 'drag-button',
	props: {
		text: {
			type: String,
			default: () => {return ''}
		},
		styleBtn: {
			type: Object,
			default: () => {
				return {
					background: 'linear-gradient(90deg, #02BE6A 0%, #009984 100%)',
					'box-shadow': '3rpx 5rpx 7rpx 0rpx rgba(11, 173, 87, 0.29)',
					bottom: '200rpx'
				}
			},
		},
		isDock: {
			type: Boolean,
			default: false
		},
		existTabBar: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			top: 0,
			left: 0,
			width: 0,
			height: 0,
			offsetWidth: 0,
			offsetHeight: 0,
			windowWidth: 0,
			windowHeight: 0,
			isMove: true,
			edge: 10,
		};
	},
	mounted() {
		const sys = uni.getSystemInfoSync();

		this.windowWidth = sys.windowWidth;
		this.windowHeight = sys.windowHeight;

		// #ifdef APP-PLUS
		this.existTabBar && (this.windowHeight -= 50);
		// #endif
		if (sys.windowTop) {
			this.windowHeight += sys.windowTop;
		}
		// console.log(sys);
		const query = uni.createSelectorQuery().in(this);
		query
			.select('#_drag_button')
			.boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				this.left = this.windowWidth - this.width - this.edge;
				this.top = ((this.windowHeight - this.height - this.edge) * 3) / 4;
			})
			.exec();
	},
	methods: {
		jumpUrl() {
			let _self = this
			_self.$emit('jump')
		},
		touchstart(e) {
			this.$emit('btnTouchstart');
		},
		touchmove(e) {
			// 单指触摸
			if (e.touches.length !== 1) {
				return false;
			}

			this.isMove = true;

			this.left = e.touches[0].clientX - this.offsetWidth;

			let clientY = e.touches[0].clientY - this.offsetHeight;
			// #ifdef H5
			clientY += this.height;
			// #endif
			let edgeBottom = this.windowHeight - this.height - this.edge;

			// 上下触及边界
			if (clientY < this.edge) {
				this.top = this.edge;
			} else if (clientY > edgeBottom) {
				this.top = edgeBottom;
			} else {
				this.top = clientY;
			}
		},
		touchend(e) {
			if (this.isDock) {
				let edgeRigth = this.windowWidth - this.width - this.edge;

				if (this.left < this.windowWidth / 2 - this.offsetWidth) {
					this.left = this.edge;
				} else {
					this.left = edgeRigth;
				}
			}

			this.isMove = false;

			this.$emit('btnTouchend');
		}
	}
};
</script>

<style lang="scss">
.drag {
	display: flex;
	justify-content: center;
	align-items: center;
	width: 110rpx;
	height: 110rpx;
	border-radius: 50%;
	font-size: 26rpx;
	position: fixed;
	z-index: 99;

	&.transition {
		transition: left 0.3s ease, top 0.3s ease;
	}
}
</style>

